package com.springboot.demo.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.springboot.demo.common.customValidator.ListNotHasNull;
import com.springboot.demo.entity.Device;
import com.springboot.demo.entity.IdList;
import com.springboot.demo.entity.User;
import com.springboot.demo.service.UserService;
import com.springboot.demo.utils.Constant;
import com.springboot.demo.utils.ResponseResult;
import com.springboot.demo.utils.ResponseUtils;
import com.springboot.demo.utils.ThreadPoolUtils;
import io.swagger.annotations.Api;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.print.attribute.standard.JobKOctets;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author wubc683
 * @desc
 * @date 2021-07-22 16:30
 */
@Controller
@Api(tags = "测试接口")
@RequestMapping("/test")
@Validated
public class TestController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping(value = "/admin")
    @ResponseBody
    public String getAdmin(/*@RequestParam("pwd") String pwd,*/
//                                  @RequestBody String body,
//                                  RedirectAttributes redirectAttributes,
            HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
//        redirectAttributes.addAttribute("pwd",pwd);
//        redirectAttributes.addAttribute("body",body);
//        String requestedSessionId = request.getRequestedSessionId();
//        Cookie[] cookies = request.getCookies();
//        System.out.println(cookies[0].getValue());
//        System.out.println(requestedSessionId);
//        redirectAttributes.addAttribute("aaa","123");
        response.setContentType("text/html;charset=UTF-8");
        request.getRequestDispatcher("/test/admin1").forward(request, response);
//        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/test/admin1");
//        requestDispatcher.forward(request,response);
//        return aa;
        return "123";
    }

    @GetMapping(value = "/admin1")
    @ResponseBody
    public void postAdmin(@RequestParam("pwd") String pwd,
                          HttpServletRequest request,
                          HttpServletResponse response) throws IOException {
        String requestedSessionId = request.getRequestedSessionId();
        Cookie[] cookies = request.getCookies();
        System.out.println(cookies[0].getValue());
        System.out.println(requestedSessionId);
        System.out.println(pwd);
        PrintWriter out = response.getWriter();
        response.setStatus(200);
//        response.setContentType("text/html;charset=UTF-8");
        Map<String, Object> map = new HashMap<>();
        map.put("code", 111);
        map.put("message", "登录成功");
        ObjectMapper om = new ObjectMapper();
        out.write(om.writeValueAsString(map));
        out.flush();
        out.close();
//        ResponseUtils responseUtils = new ResponseUtils();
//        response.getWriter().write(111);
//        return responseUtils.buildFailedResponse(ResponseResult.USER_NOT_LOGIN);
    }

    @PutMapping("/admin")
    @ResponseBody
    public String putAdmin(@RequestBody List<Long> ids) {
        return "put admin";
    }

    @DeleteMapping("/admin")
    @ResponseBody
    public String deleteAdmin(@RequestBody List<Long> ids) {
        System.out.println(ids);
        return "delete admin";
    }

    @PostMapping("/abcd")
    @ResponseBody
    public String abc(@Valid @RequestBody Float d) {
        System.out.println(d);
        System.out.println(Float.isFinite(d));
//        Double aDouble = Double.valueOf(d);
//        System.out.println(aDouble);
//        long l = aDouble.longValue();
//        System.out.println(l);
        return "111";
    }

    @PostMapping("/abc111")
    @ResponseBody
    public String abc111(@RequestBody JSONObject str1) {
        System.out.println(str1);
//        String[] split = str.split(",");
//        System.out.println(split[0]);
//        System.out.println(split[1]);
//        System.out.println(split[2]);
        String[] aa = {"555", "444", "333"};
        return Arrays.toString(aa);
    }

    @GetMapping("/rbt")
    @ResponseBody
    public String test_rbt() {
//        int maximumPoolSize = 110;
//        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(maximumPoolSize-1,
//                maximumPoolSize,
//                maximumPoolSize-10,
//                TimeUnit.SECONDS,
//                new ArrayBlockingQueue<>(50),
//                Executors.defaultThreadFactory(),
//                new ThreadPoolExecutor.AbortPolicy());
//        for (int i = 0; i < 100; i++) {
//            threadPoolExecutor.execute(new Runnable() {
//                @Override
//                public void run() {
//                    JSONObject js = new JSONObject();
//                    js.put("code",1);
////                    js.put("code", 40002);
////                    js.put("userId", 360);
////                    js.put("sceneId", 1472);
////                    js.put("productTime", "2022-05-10 14:10:33");
////                    JSONArray arr = new JSONArray();
////                    JSONObject v = new JSONObject();
////                    v.put("varId", 11111);
////                    v.put("value", 33333);
////                    arr.add(v);
////                    js.put("var", arr);
////                    js.put("alarm", new JSONArray());
//
//                    for (int i = 0; i < 300; i++) {
////                        int suffix = (i%5)+1;
//                        rabbitTemplate.convertAndSend(Constant.Rabbitmq.CLIENT_TO_WEB_EXCHANGE,
//                                Constant.Rabbitmq.CLIENT_TO_WEB_ROUTING_KEY,
//                                js, message -> {
//                                    message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
//                                    return message;
//                                });
//                    }
//                }
//            });
//        }

        return "ok";
    }

}
